package com.qf.dao.Impl;


import com.qf.dao.UserLoginDao;
import com.qf.entity.User;
import com.qf.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

public class UserLoginDaoImpl implements UserLoginDao {
    private QueryRunner queryRunner = new QueryRunner();

    @Override
    public User selectUser(String username) {
        try {
            return queryRunner.query(DruidUtil.getConnection(), "select * from user where username=?", new BeanHandler<>(User.class), username);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "查询单个用户时出现异常");
        }
    }

    @Override
    public int updatePassword(String rePassword, String username) {

        try {
            return queryRunner.update(DruidUtil.getConnection(), "update user set password=? where username=?", rePassword, username);
        } catch (SQLException e) {
            throw new RuntimeException(e.getMessage() + "修改密码出现错误");
        }
    }
}
